﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Easyleave.Logic.Membership;
using Easyleave.Logic.Domain;
using Easyleave.Logic.Validation;

namespace Easyleave.Workflow
{
	public sealed partial class LeaveWorkflow: StateMachineWorkflowActivity
	{
        

		public LeaveWorkflow()
		{
			InitializeComponent();
  		}

        public LeaveDto GetLeave ()
        {
            LeaveDataManager dataManager = new LeaveDataManager();
            return  dataManager.GetRecord(WorkflowInstanceId);
        }

        private bool IsLeaveValid() 
        {
            LeaveDto leave = GetLeave();
            IValidation validation = ValidationFactory.GetValidation();
            int totalLeaves = validation.CountTotalEntitlement(leave.Owner.Id, leave.LeaveType.Trim());
            if (totalLeaves > leave.LeaveDays)
            {
                return true;
            }

            return false;
        }

        private void recommendationEventHandler_Invoked(object sender, ExternalDataEventArgs e)
        {
            //this.CurrentStateName
            LeaveDataManager dataManager = new LeaveDataManager();
            LeaveDto leave = dataManager.GetRecord(WorkflowInstanceId);
            Guid? recommenderId = MembershipFactory.GetMembership().GetNextRecommender(leave.Owner.Id, leave.NextRecommender.Id);
            dataManager.UpdateNextRecommenderId(WorkflowInstanceId, recommenderId);
        }

        private void submissionEventHandler_Invoked(object sender, ExternalDataEventArgs e)
        {
            IMembership membership = MembershipFactory.GetMembership();
            LeaveDto leave = ((LeaveSubmittedEventArgs)e).Leave;
            leave.Owner = membership.GetUser(((LeaveSubmittedEventArgs)e).AuthorId);
            leave.NextRecommender = membership.GetUser(membership.GetNextRecommender(leave.Owner.Id, Guid.Empty));
            leave.Approver = membership.GetUser(membership.GetApprover(leave.Owner.Id));
            leave.WorkflowInstanceId = WorkflowInstanceId;
            leave.CurrentStateName = StateNames.submittedState.ToString();
            leave.LeaveDays = ValidationFactory.GetValidation().CountLeaves((DateTime)leave.FromDate, (DateTime)leave.ToDate);

            LeaveDataManager dataManager = new LeaveDataManager();
            dataManager.InsertRecord(leave);
        }

        private void changeExtState_ExecuteCode(object sender, EventArgs e)
        {
            LeaveDataManager dataManager = new LeaveDataManager();
            dataManager.UpdateCurrentState(WorkflowInstanceId, CurrentStateName);
        }


	}

}
